package com.zhihaoliang.map.home

import androidx.compose.animation.animateColor
import androidx.compose.animation.core.MutableTransitionState
import androidx.compose.animation.core.Transition
import androidx.compose.animation.core.tween
import androidx.compose.animation.core.updateTransition
import androidx.compose.foundation.layout.Column
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import com.zhihaoliang.map.R
import com.zhihaoliang.map.base.CraneEditableUserInput
import com.zhihaoliang.map.base.CraneUserInput
import com.zhihaoliang.map.base.rememberEditableUserInputState
import com.zhihaoliang.map.ui.theme.CraneTheme
import kotlinx.coroutines.flow.filter

/**
 * @author 支豪亮
 * @date 2023/1/12 16:48
 */

enum class PeopleUserInputAnimationState{Valid,Invalid}

/**
 * 同行人数的变化的状态
 */
class PeopleUserInputState{
    var people by mutableStateOf(1)
    private set

    val animationState:MutableTransitionState<PeopleUserInputAnimationState> = MutableTransitionState(PeopleUserInputAnimationState.Valid)

    fun addPerson(){
        people = (people % (MAX_PEOPLE + 1)) + 1
        updateAnimationState()
    }

    private fun updateAnimationState(){
        val newState = if(people > MAX_PEOPLE) PeopleUserInputAnimationState.Invalid else PeopleUserInputAnimationState.Valid
        if(animationState.currentState != newState) animationState.targetState = newState
    }
}

@Composable
fun PeopleUserInput(
    titleSuffix: String? = "",
    onPeopleChanged: (Int) -> Unit
) {
    val peopleState: PeopleUserInputState = remember { PeopleUserInputState() }
    Column {
        val transitionState:MutableTransitionState<PeopleUserInputAnimationState> = remember { peopleState.animationState }
        val tint: State<Color> = tintPeopleUserInput(transitionState)

        val people:Int = peopleState.people
        CraneUserInput(
            text = if (people == 1) "$people Adult$titleSuffix" else "$people Adults$titleSuffix",
            vectorImageId = R.drawable.ic_person,
            tint = tint.value,
            onClick = {
                peopleState.addPerson()
                onPeopleChanged(peopleState.people)
            }
        )

        if (transitionState.targetState == PeopleUserInputAnimationState.Invalid) {
            Text(
                text = "Error: We don't support more than $MAX_PEOPLE people",
                style = MaterialTheme.typography.body1.copy(color = MaterialTheme.colors.secondary)
            )
        }
    }
}


@Composable
fun  FromDestination(){
    CraneUserInput(text = "Seoul, South Korea", vectorImageId = R.drawable.ic_location)
}

@Composable
fun ToDestinationUserInput(onToDestinationChanged:(String)->Unit){
    val editableUserInputState = rememberEditableUserInputState(hint = "Choose Destination")
    CraneEditableUserInput(state = editableUserInputState,
                           caption="To",
                           vectorImageId = R.drawable.ic_plane )

    val currentOnDestinationChanged by rememberUpdatedState(onToDestinationChanged)
    LaunchedEffect(editableUserInputState) {
        snapshotFlow { editableUserInputState.text }
            .filter { !editableUserInputState.isHint }
            .collect {
                currentOnDestinationChanged(editableUserInputState.text)
            }
    }
}

@Composable
fun DatesUserInput(){
    CraneUserInput(caption = "Select Dates", text = "", vectorImageId = R.drawable.ic_calendar)
}


@Composable
private fun tintPeopleUserInput( transitionState: MutableTransitionState<PeopleUserInputAnimationState>): State<Color> {
    val validColor = MaterialTheme.colors.onSurface
    val invalidColor = MaterialTheme.colors.secondary

    val transition: Transition<PeopleUserInputAnimationState> = updateTransition(transitionState, label = "")
    return transition.animateColor(
        transitionSpec = { tween(durationMillis = 300) }, label = ""
    ) {
        if (it == PeopleUserInputAnimationState.Valid) validColor else invalidColor
    }
}


@Composable
@Preview
private fun PeopleUserInputPreview(){
    CraneTheme {
        PeopleUserInput(onPeopleChanged = {}, titleSuffix = "Economy")
    }
}